package com.zmxstudy.rbac.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zmxstudy.rbac.entity.Auth;
import com.zmxstudy.rbac.entity.RoleAuth;
import com.zmxstudy.rbac.mapper.RoleMapper;
import com.zmxstudy.rbac.mapper.UserMapper;
import com.zmxstudy.rbac.service.AuthService;
import com.zmxstudy.rbac.mapper.AuthMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
* @author star
* @description 针对表【auth(系统菜单)】的数据库操作Service实现
* @createDate 2024-04-03 15:37:14
*/
@Service
public class AuthServiceImpl extends ServiceImpl<AuthMapper, Auth>
    implements AuthService{
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public List<Auth> getMenu(String username) {
        System.out.println(username);
//      寻找一级菜单
       Long userId = userMapper.selectIdByUsername(username);
       System.out.println(userId);
       ArrayList<Long> roleIds = (ArrayList<Long>) roleMapper.findRoleIds(userId);
       System.out.println(roleIds);

       List<Long> authIds = baseMapper.findAuthIds(roleIds);
       System.out.println(authIds);


        List<Auth> auths = baseMapper.selectMenuByUsername(authIds);
        LinkedList<Auth> menus = new LinkedList<>();
        auths.forEach(auth -> {
            if(auth.getPid()==0){
                menus.add(auth);
            }
        });

//        寻找二级菜单
        menus.forEach(auth -> {
            final LinkedList<Auth> children = new LinkedList<>();
            auths.forEach(auth1 -> {
                if(auth1.getPid().equals(auth.getId())){
                    children.add(auth1);
                }
            });

            auth.setChildren(children);
        });

        return menus;
    }

    @Override
    public List<RoleAuth> AllRoleAuths() {
        return baseMapper.AllRoleAuths();
    }

    @Override
    public boolean delRoleAuth(int roleId, int authId) {
        return baseMapper.delRoleAuth(roleId,authId);
    }

    @Override
    public boolean plusRoleAuth(int roleId, int authId) {
        return baseMapper.plusRoleAuth(roleId,authId);
    }
}




